﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using BesAsm.MasterData.Entities;
using BesAsm.MasterData.Data;

#endregion

namespace BesAsm.MasterData.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="CrossSectionProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class CrossSectionProviderBaseCore : EntityProviderBase<BesAsm.MasterData.Entities.CrossSection, BesAsm.MasterData.Entities.CrossSectionKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, BesAsm.MasterData.Entities.CrossSectionKey key)
		{
			return Delete(transactionManager, key.CrossSectionId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_crossSectionId">Primary key, cross section id. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _crossSectionId)
		{
			return Delete(null, _crossSectionId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_crossSectionId">Primary key, cross section id. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _crossSectionId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		FK_CROSS_SECTIONS_LINKS Description: 
		/// </summary>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByLinkId(System.Int32? _linkId)
		{
			int count = -1;
			return GetByLinkId(_linkId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		FK_CROSS_SECTIONS_LINKS Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		/// <remarks></remarks>
		public TList<CrossSection> GetByLinkId(TransactionManager transactionManager, System.Int32? _linkId)
		{
			int count = -1;
			return GetByLinkId(transactionManager, _linkId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		FK_CROSS_SECTIONS_LINKS Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByLinkId(TransactionManager transactionManager, System.Int32? _linkId, int start, int pageLength)
		{
			int count = -1;
			return GetByLinkId(transactionManager, _linkId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		fkCrossSectionsLinks Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByLinkId(System.Int32? _linkId, int start, int pageLength)
		{
			int count =  -1;
			return GetByLinkId(null, _linkId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		fkCrossSectionsLinks Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByLinkId(System.Int32? _linkId, int start, int pageLength,out int count)
		{
			return GetByLinkId(null, _linkId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_LINKS key.
		///		FK_CROSS_SECTIONS_LINKS Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_linkId">Foreign key to LINKS</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public abstract TList<CrossSection> GetByLinkId(TransactionManager transactionManager, System.Int32? _linkId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		FK_CROSS_SECTIONS_STATUSES Description: 
		/// </summary>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByStatus(System.String _status)
		{
			int count = -1;
			return GetByStatus(_status, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		FK_CROSS_SECTIONS_STATUSES Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		/// <remarks></remarks>
		public TList<CrossSection> GetByStatus(TransactionManager transactionManager, System.String _status)
		{
			int count = -1;
			return GetByStatus(transactionManager, _status, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		FK_CROSS_SECTIONS_STATUSES Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByStatus(TransactionManager transactionManager, System.String _status, int start, int pageLength)
		{
			int count = -1;
			return GetByStatus(transactionManager, _status, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		fkCrossSectionsStatuses Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByStatus(System.String _status, int start, int pageLength)
		{
			int count =  -1;
			return GetByStatus(null, _status, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		fkCrossSectionsStatuses Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public TList<CrossSection> GetByStatus(System.String _status, int start, int pageLength,out int count)
		{
			return GetByStatus(null, _status, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_CROSS_SECTIONS_STATUSES key.
		///		FK_CROSS_SECTIONS_STATUSES Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_status">Status of record, in terms of usability for further analysis and general state of data</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of BesAsm.MasterData.Entities.CrossSection objects.</returns>
		public abstract TList<CrossSection> GetByStatus(TransactionManager transactionManager, System.String _status, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override BesAsm.MasterData.Entities.CrossSection Get(TransactionManager transactionManager, BesAsm.MasterData.Entities.CrossSectionKey key, int start, int pageLength)
		{
			return GetByCrossSectionId(transactionManager, key.CrossSectionId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(System.Int32 _crossSectionId)
		{
			int count = -1;
			return GetByCrossSectionId(null,_crossSectionId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(System.Int32 _crossSectionId, int start, int pageLength)
		{
			int count = -1;
			return GetByCrossSectionId(null, _crossSectionId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(TransactionManager transactionManager, System.Int32 _crossSectionId)
		{
			int count = -1;
			return GetByCrossSectionId(transactionManager, _crossSectionId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(TransactionManager transactionManager, System.Int32 _crossSectionId, int start, int pageLength)
		{
			int count = -1;
			return GetByCrossSectionId(transactionManager, _crossSectionId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(System.Int32 _crossSectionId, int start, int pageLength, out int count)
		{
			return GetByCrossSectionId(null, _crossSectionId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_CROSS_SECTIONS index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_crossSectionId">Primary key, cross section id</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.CrossSection"/> class.</returns>
		public abstract BesAsm.MasterData.Entities.CrossSection GetByCrossSectionId(TransactionManager transactionManager, System.Int32 _crossSectionId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;CrossSection&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;CrossSection&gt;"/></returns>
		public static TList<CrossSection> Fill(IDataReader reader, TList<CrossSection> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				BesAsm.MasterData.Entities.CrossSection c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("CrossSection")
					.Append("|").Append((System.Int32)reader[((int)CrossSectionColumn.CrossSectionId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<CrossSection>(
					key.ToString(), // EntityTrackingKey
					"CrossSection",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new BesAsm.MasterData.Entities.CrossSection();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.CrossSectionId = (System.Int32)reader[((int)CrossSectionColumn.CrossSectionId - 1)];
					c.OriginalCrossSectionId = c.CrossSectionId;
					c.LinkId = (reader.IsDBNull(((int)CrossSectionColumn.LinkId - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.LinkId - 1)];
					c.RoughnessMain = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessMain - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessMain - 1)];
					c.RoughnessLeft = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessLeft - 1)];
					c.RoughnessRight = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessRight - 1)];
					c.StationLeft = (reader.IsDBNull(((int)CrossSectionColumn.StationLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.StationLeft - 1)];
					c.StationRight = (reader.IsDBNull(((int)CrossSectionColumn.StationRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.StationRight - 1)];
					c.LengthFactorLeft = (reader.IsDBNull(((int)CrossSectionColumn.LengthFactorLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.LengthFactorLeft - 1)];
					c.LengthFactorRight = (reader.IsDBNull(((int)CrossSectionColumn.LengthFactorRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.LengthFactorRight - 1)];
					c.EditedBy = (System.String)reader[((int)CrossSectionColumn.EditedBy - 1)];
					c.EditReason = (System.String)reader[((int)CrossSectionColumn.EditReason - 1)];
					c.Status = (System.String)reader[((int)CrossSectionColumn.Status - 1)];
					c.AppendDate = (System.DateTime)reader[((int)CrossSectionColumn.AppendDate - 1)];
					c.EditDate = (reader.IsDBNull(((int)CrossSectionColumn.EditDate - 1)))?null:(System.DateTime?)reader[((int)CrossSectionColumn.EditDate - 1)];
					c.MstXsectionsIdDeprecated = (reader.IsDBNull(((int)CrossSectionColumn.MstXsectionsIdDeprecated - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.MstXsectionsIdDeprecated - 1)];
					c.MlinkidDeprecated = (reader.IsDBNull(((int)CrossSectionColumn.MlinkidDeprecated - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.MlinkidDeprecated - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="BesAsm.MasterData.Entities.CrossSection"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.CrossSection"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, BesAsm.MasterData.Entities.CrossSection entity)
		{
			if (!reader.Read()) return;
			
			entity.CrossSectionId = (System.Int32)reader[((int)CrossSectionColumn.CrossSectionId - 1)];
			entity.OriginalCrossSectionId = (System.Int32)reader["cross_section_id"];
			entity.LinkId = (reader.IsDBNull(((int)CrossSectionColumn.LinkId - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.LinkId - 1)];
			entity.RoughnessMain = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessMain - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessMain - 1)];
			entity.RoughnessLeft = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessLeft - 1)];
			entity.RoughnessRight = (reader.IsDBNull(((int)CrossSectionColumn.RoughnessRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.RoughnessRight - 1)];
			entity.StationLeft = (reader.IsDBNull(((int)CrossSectionColumn.StationLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.StationLeft - 1)];
			entity.StationRight = (reader.IsDBNull(((int)CrossSectionColumn.StationRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.StationRight - 1)];
			entity.LengthFactorLeft = (reader.IsDBNull(((int)CrossSectionColumn.LengthFactorLeft - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.LengthFactorLeft - 1)];
			entity.LengthFactorRight = (reader.IsDBNull(((int)CrossSectionColumn.LengthFactorRight - 1)))?null:(System.Decimal?)reader[((int)CrossSectionColumn.LengthFactorRight - 1)];
			entity.EditedBy = (System.String)reader[((int)CrossSectionColumn.EditedBy - 1)];
			entity.EditReason = (System.String)reader[((int)CrossSectionColumn.EditReason - 1)];
			entity.Status = (System.String)reader[((int)CrossSectionColumn.Status - 1)];
			entity.AppendDate = (System.DateTime)reader[((int)CrossSectionColumn.AppendDate - 1)];
			entity.EditDate = (reader.IsDBNull(((int)CrossSectionColumn.EditDate - 1)))?null:(System.DateTime?)reader[((int)CrossSectionColumn.EditDate - 1)];
			entity.MstXsectionsIdDeprecated = (reader.IsDBNull(((int)CrossSectionColumn.MstXsectionsIdDeprecated - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.MstXsectionsIdDeprecated - 1)];
			entity.MlinkidDeprecated = (reader.IsDBNull(((int)CrossSectionColumn.MlinkidDeprecated - 1)))?null:(System.Int32?)reader[((int)CrossSectionColumn.MlinkidDeprecated - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="BesAsm.MasterData.Entities.CrossSection"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.CrossSection"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, BesAsm.MasterData.Entities.CrossSection entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.CrossSectionId = (System.Int32)dataRow["cross_section_id"];
			entity.OriginalCrossSectionId = (System.Int32)dataRow["cross_section_id"];
			entity.LinkId = Convert.IsDBNull(dataRow["link_id"]) ? null : (System.Int32?)dataRow["link_id"];
			entity.RoughnessMain = Convert.IsDBNull(dataRow["roughness_main"]) ? null : (System.Decimal?)dataRow["roughness_main"];
			entity.RoughnessLeft = Convert.IsDBNull(dataRow["roughness_left"]) ? null : (System.Decimal?)dataRow["roughness_left"];
			entity.RoughnessRight = Convert.IsDBNull(dataRow["roughness_right"]) ? null : (System.Decimal?)dataRow["roughness_right"];
			entity.StationLeft = Convert.IsDBNull(dataRow["station_left"]) ? null : (System.Decimal?)dataRow["station_left"];
			entity.StationRight = Convert.IsDBNull(dataRow["station_right"]) ? null : (System.Decimal?)dataRow["station_right"];
			entity.LengthFactorLeft = Convert.IsDBNull(dataRow["length_factor_left"]) ? null : (System.Decimal?)dataRow["length_factor_left"];
			entity.LengthFactorRight = Convert.IsDBNull(dataRow["length_factor_right"]) ? null : (System.Decimal?)dataRow["length_factor_right"];
			entity.EditedBy = (System.String)dataRow["edited_by"];
			entity.EditReason = (System.String)dataRow["edit_reason"];
			entity.Status = (System.String)dataRow["status"];
			entity.AppendDate = (System.DateTime)dataRow["append_date"];
			entity.EditDate = Convert.IsDBNull(dataRow["edit_date"]) ? null : (System.DateTime?)dataRow["edit_date"];
			entity.MstXsectionsIdDeprecated = Convert.IsDBNull(dataRow["mst_xsections_id_deprecated"]) ? null : (System.Int32?)dataRow["mst_xsections_id_deprecated"];
			entity.MlinkidDeprecated = Convert.IsDBNull(dataRow["mlinkid_deprecated"]) ? null : (System.Int32?)dataRow["mlinkid_deprecated"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.CrossSection"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">BesAsm.MasterData.Entities.CrossSection Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, BesAsm.MasterData.Entities.CrossSection entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region LinkIdSource	
			if (CanDeepLoad(entity, "Link|LinkIdSource", deepLoadType, innerList) 
				&& entity.LinkIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.LinkId ?? (int)0);
				Link tmpEntity = EntityManager.LocateEntity<Link>(EntityLocator.ConstructKeyFromPkItems(typeof(Link), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.LinkIdSource = tmpEntity;
				else
					entity.LinkIdSource = DataRepository.LinkProvider.GetByLinkId(transactionManager, (entity.LinkId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'LinkIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.LinkIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.LinkProvider.DeepLoad(transactionManager, entity.LinkIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion LinkIdSource

			#region StatusSource	
			if (CanDeepLoad(entity, "Status|StatusSource", deepLoadType, innerList) 
				&& entity.StatusSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.Status;
				Status tmpEntity = EntityManager.LocateEntity<Status>(EntityLocator.ConstructKeyFromPkItems(typeof(Status), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.StatusSource = tmpEntity;
				else
					entity.StatusSource = DataRepository.StatusProvider.GetByStatusId(transactionManager, entity.Status);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'StatusSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.StatusSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.StatusProvider.DeepLoad(transactionManager, entity.StatusSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion StatusSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByCrossSectionId methods when available
			
			#region CrossSectionDatumCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<CrossSectionDatum>|CrossSectionDatumCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CrossSectionDatumCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.CrossSectionDatumCollection = DataRepository.CrossSectionDatumProvider.GetByCrossSectionId(transactionManager, entity.CrossSectionId);

				if (deep && entity.CrossSectionDatumCollection.Count > 0)
				{
					deepHandles.Add("CrossSectionDatumCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<CrossSectionDatum>) DataRepository.CrossSectionDatumProvider.DeepLoad,
						new object[] { transactionManager, entity.CrossSectionDatumCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the BesAsm.MasterData.Entities.CrossSection object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">BesAsm.MasterData.Entities.CrossSection instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">BesAsm.MasterData.Entities.CrossSection Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, BesAsm.MasterData.Entities.CrossSection entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region LinkIdSource
			if (CanDeepSave(entity, "Link|LinkIdSource", deepSaveType, innerList) 
				&& entity.LinkIdSource != null)
			{
				DataRepository.LinkProvider.Save(transactionManager, entity.LinkIdSource);
				entity.LinkId = entity.LinkIdSource.LinkId;
			}
			#endregion 
			
			#region StatusSource
			if (CanDeepSave(entity, "Status|StatusSource", deepSaveType, innerList) 
				&& entity.StatusSource != null)
			{
				DataRepository.StatusProvider.Save(transactionManager, entity.StatusSource);
				entity.Status = entity.StatusSource.StatusId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<CrossSectionDatum>
				if (CanDeepSave(entity.CrossSectionDatumCollection, "List<CrossSectionDatum>|CrossSectionDatumCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(CrossSectionDatum child in entity.CrossSectionDatumCollection)
					{
						if(child.CrossSectionIdSource != null)
						{
							child.CrossSectionId = child.CrossSectionIdSource.CrossSectionId;
						}
						else
						{
							child.CrossSectionId = entity.CrossSectionId;
						}

					}

					if (entity.CrossSectionDatumCollection.Count > 0 || entity.CrossSectionDatumCollection.DeletedItems.Count > 0)
					{
						//DataRepository.CrossSectionDatumProvider.Save(transactionManager, entity.CrossSectionDatumCollection);
						
						deepHandles.Add("CrossSectionDatumCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< CrossSectionDatum >) DataRepository.CrossSectionDatumProvider.DeepSave,
							new object[] { transactionManager, entity.CrossSectionDatumCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region CrossSectionChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>BesAsm.MasterData.Entities.CrossSection</c>
	///</summary>
	public enum CrossSectionChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Link</c> at LinkIdSource
		///</summary>
		[ChildEntityType(typeof(Link))]
		Link,
			
		///<summary>
		/// Composite Property for <c>Status</c> at StatusSource
		///</summary>
		[ChildEntityType(typeof(Status))]
		Status,
	
		///<summary>
		/// Collection of <c>CrossSection</c> as OneToMany for CrossSectionDatumCollection
		///</summary>
		[ChildEntityType(typeof(TList<CrossSectionDatum>))]
		CrossSectionDatumCollection,
	}
	
	#endregion CrossSectionChildEntityTypes
	
	#region CrossSectionFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;CrossSectionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CrossSection"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CrossSectionFilterBuilder : SqlFilterBuilder<CrossSectionColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CrossSectionFilterBuilder class.
		/// </summary>
		public CrossSectionFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CrossSectionFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CrossSectionFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CrossSectionFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CrossSectionFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CrossSectionFilterBuilder
	
	#region CrossSectionParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;CrossSectionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CrossSection"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CrossSectionParameterBuilder : ParameterizedSqlFilterBuilder<CrossSectionColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CrossSectionParameterBuilder class.
		/// </summary>
		public CrossSectionParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CrossSectionParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CrossSectionParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CrossSectionParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CrossSectionParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CrossSectionParameterBuilder
	
	#region CrossSectionSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;CrossSectionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CrossSection"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class CrossSectionSortBuilder : SqlSortBuilder<CrossSectionColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CrossSectionSqlSortBuilder class.
		/// </summary>
		public CrossSectionSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion CrossSectionSortBuilder
	
} // end namespace
